package com.rapida.framwork;

import android.app.Activity;

import java.util.Stack;

public class ActivityManager {
	private static Stack<Activity> mActivityStack;
	private static ActivityManager mInst;

	private ActivityManager() {
	}

	public static ActivityManager getActivityManager() {
		if (mInst == null) {
			mInst = new ActivityManager();
		}
		return mInst;
	}

	public void addActivity(Activity activity) {
		if (mActivityStack == null) {
			mActivityStack = new Stack<Activity>();
		}
		mActivityStack.add(activity);
	}

	public Activity currentActivity() {
		Activity activity = mActivityStack.lastElement();
		return activity;
	}

	public void finishActivity() {
		Activity activity = mActivityStack.lastElement();
		finishActivity(activity);
	}

	public void finishActivity(Activity activity) {
		if (activity != null) {
			mActivityStack.remove(activity);
			activity.finish();
			activity = null;
		}
	}

	public void finishActivity(Class<?> cls) {
		for (Activity activity : mActivityStack) {
			if (activity.getClass().equals(cls)) {
				finishActivity(activity);
			}
		}
	}

	public void finishAllActivity() {
		for (int i = 0, size = mActivityStack.size(); i < size; i++) {
			if (null != mActivityStack.get(i)) {
				Activity activity = mActivityStack.get(i);
				if (!activity.isFinishing()) {
					activity.finish();
				}
			}
		}
		mActivityStack.clear();
	}
}
